
package com.ami.iusb;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.ami.iusb.protocol.HarddiskProtocol;
import com.ami.iusb.protocol.IUSBSCSI;
import com.ami.iusb.protocol.PacketMaster;
import com.ami.kvm.imageredir.IUSBHeader;
import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.gui.InfoDialog;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.kvm.jviewer.gui.StandAloneConnectionDialog;
import com.ami.vmedia.VMApp;

public class HarddiskRedir extends Thread {
	private PacketMaster packetMaster;
	private HarddiskProtocol protocol;
	private ByteBuffer packetReadBuffer;
	private ByteBuffer packetWriteBuffer;
	private boolean physicalDevice;
	private int HDDevice_no;
	private boolean running = false;
	private boolean stopRunning = false;
	private long nativeReaderPointer = -1L;
	private Thread redirThread;
	private String sourceHarddisk = null;
	private int nBytesRedirected = 0;
	private int hdInstanceNum;
	private boolean hdReconnect = false;

	private static final int DEVICE_REDIRECTION_ACK = 241;
	private static final int AUTH_CMD = 242;
	private static final int SET_HARDDISK_TYPE = 244;
	private static final int MAX_READ_SECTORS = 256;
	private static final int MAX_READ_SIZE = 131072;
	private boolean hdImageEjected = false;
	private boolean hdImageRedirected = false;
	private boolean hdServiceRestarted = false;
	public static final String PHYSICAL_DRIVE = LocaleStrings.getString("A_5_DP");
	public static final String LOGICAL_DRIVE = LocaleStrings.getString("A_6_DP");

	public static final int READ_WRITE = 1;
	public static final int READ_ONLY = 2;
	public static final int SECTOR_RANGE_ERROR = -1;
	public static final int ALREADY_IN_USE = -3;
	public static final int MEDIA_ERROR = -4;
	int Drive_Type;
	String[] ListDrive_USB = null;
	String[] ListDrive_Fixed = null;
	private vHarddiskMonitorThread vMThread = null;
	private String errorMessage = " ";

	private static Object syncObj = new Object();
	private boolean confModified = false;
	private boolean hdRedirectionKilled = false;
	private boolean hdStoppedByUser = false;

	private int harddiskRedirStatus;
	private int hardDiskDeviceStatus;
	private int hdStopMode;
	public MountFolder folder = null;
	public String folderPath = null;
	public String imagePath = null;
	public int imageSize = 0;
	public long ImageCreateTime = 0L;
	public boolean mount_folder_flag = false;
	public ImageProgress progress = null;

//	static {
//		try {
//			if ((!JViewer.isdownloadapp()) && (!JViewer.isplayerapp())) {
//				if (JViewer.isjviewerapp()) {
//					System.loadLibrary("javaharddiskwrapper");
//				} else {
//					loadWrapperLibrary();
//				}
//			}
//		} catch (UnsatisfiedLinkError localUnsatisfiedLinkError) {
//			System.err.println(LocaleStrings.getString("AB_2_HDREDIR"));
//		}
//	}
	
	static {
		String path = System.getProperty("user.dir") + File.separator + "native" + File.separator
				+ "javaharddiskwrapper.dll";
		System.load(path);
	}

	private static void loadWrapperLibrary() {
		String str1 = null;
		File localFile = null;

		String str2 = JViewer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		str2 = str2.substring(0, str2.lastIndexOf('/'));

		if (str2.contains("%20")) {
			str2 = str2.replaceAll("%20", "\\ ");
		}

		String str3 = File.separator + "Jar" + File.separator + JViewer.getIp() + File.separator + "lib"
				+ File.separator;

		if (System.getProperty("os.name").startsWith("Windows")) {
			if ((JViewer.isStandAloneApp()) || (JViewer.isVMApp())) {
				str3 = StandAloneConnectionDialog.replaceAllPattern(str3, ":", "_");
				str1 = str2 + str3 + "javaharddiskwrapper.dll";
				localFile = new File(str1);
				if (false == StandAloneConnectionDialog.getWrapperLibrary("javaharddiskwrapper.dll")) {
					Debug.out.println("Unable to extract the javaharddiskwrapper.dll");
					str1 = null;
				}
			} else {
				str1 = System.getProperty("user.dir") + str3 + "javaharddiskwrapper.dll";
			}
		} else if (System.getProperty("os.name").startsWith("Linux")) {
			if ((JViewer.isStandAloneApp()) || (JViewer.isVMApp())) {
				str1 = str2 + str3 + "libjavaharddiskwrapper.so";
				localFile = new File(str1);
				if (false == StandAloneConnectionDialog.getWrapperLibrary("libjavaharddiskwrapper.so")) {
					Debug.out.println("Unable to extract the libjavaharddiskwrapper.so");
					str1 = null;
				}
			} else {
				str1 = System.getProperty("user.dir") + str3 + "libjavaharddiskwrapper.so";
			}
		} else if (System.getProperty("os.name").startsWith("Mac"))
			if ((JViewer.isStandAloneApp()) || (JViewer.isVMApp())) {
				str1 = str2 + str3 + "libjavaharddiskwrapper.jnilib";
				localFile = new File(str1);
				if (false == StandAloneConnectionDialog.getWrapperLibrary("libjavaharddiskwrapper.jnilib")) {
					Debug.out.println("Unable to extract the libjavaharddiskwrapper.jnlib");
					str1 = null;
				}
			} else {
				str1 = System.getProperty("user.dir") + str3 + "libjavaharddiskwrapper.jnilib";
			}
		try {
			System.load(str1);
		} catch (UnsatisfiedLinkError localUnsatisfiedLinkError) {
			Debug.out.println(localUnsatisfiedLinkError);
			showLibraryLoadError();
		} catch (Exception localException) {
			Debug.out.println(localException);
			showLibraryLoadError();
		}
	}

	public HarddiskRedir(boolean paramBoolean) {
		this.physicalDevice = paramBoolean;
		this.harddiskRedirStatus = 0;
		this.hardDiskDeviceStatus = 1;
		this.hdStopMode = 0;
		this.protocol = new HarddiskProtocol();
		this.packetReadBuffer = ByteBuffer.allocateDirect(131134);

		this.packetWriteBuffer = ByteBuffer.allocateDirect(131134);
	}

	private int harddiskConnect(String paramString, int paramInt, boolean paramBoolean) throws IOException {
		this.packetMaster = new PacketMaster(paramString, paramInt, false, this.protocol, paramBoolean);
		this.packetMaster.setupBuffers(this.packetReadBuffer, this.packetWriteBuffer);
		this.packetMaster.setBufferEndianness(ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);

		if (JViewer.isSinglePortEnabled()) {
			if (JViewerApp.getInstance().getSinglePortKvm().setHTTPConnect("HDMEDIA") < 0) {
				setErrorMessage(
						LocaleStrings.getString("AB_3_HDREDIR") + paramInt + LocaleStrings.getString("AB_4_HDREDIR"));

				return -1;
			}
			this.packetMaster.setSock(JViewerApp.getInstance().getSinglePortKvm().getHttpsock());

		} else if (this.packetMaster.connectVmedia(paramBoolean) < 0) {
			setErrorMessage(
					LocaleStrings.getString("AB_3_HDREDIR") + paramInt + LocaleStrings.getString("AB_4_HDREDIR"));

			return -1;
		}

		return 0;
	}

	private void harddiskDisconnect() {
		try {
			this.packetMaster.VmediaSockclose();
		} catch (IOException localIOException) {
			System.err.println(LocaleStrings.getString("AB_5_HDREDIR") + localIOException.getMessage());
		}
	}

	public void stopCreateImageProgress() {
		if (this.progress != null) {
			this.progress.stopProgress();
		}
	}

	public boolean createImageProgressBar(int paramInt) {
		JLabel localJLabel = null;
		JProgressBar localJProgressBar = new JProgressBar(0, 100);
		Object localObject = null;
		JButton localJButton = new JButton("Cancel");
		JPanel localJPanel = new JPanel();
		Object[] arrayOfObject = { "cancle" };

		int i = 0;

		this.progress = new ImageProgress(this.imageSize, this.folderPath, this.imagePath, localJProgressBar);
		localJPanel.setLayout(new BorderLayout());
		String str1;
		String str2;
		if (paramInt == 1) {
			str1 = "Copying File...";
			str2 = "Create Image Progress";
			this.progress.setActionFlag(1);

			if (!this.progress.getImageSizeCheck()) {
				this.progress = null;
				System.gc();
				setErrorMessage("Image Size is too small");
				return false;
			}
		} else {
			int j = JOptionPane.showConfirmDialog(null, "Synchronize to Disk?", "Warning", 0);

			if (j == 1) {

				this.progress = null;
				System.gc();
				return true;
			}

			str1 = "Synchronizing File...";
			str2 = "Synchronize Disk Progress";
			this.progress.setActionFlag(2);
			this.progress.setImageCreateTime(this.ImageCreateTime);
		}

		localJLabel = new JLabel(str1);
		localJProgressBar.setValue(0);
		localJPanel.add(localJLabel, "North");
		localJPanel.add(localJProgressBar, "Center");

		this.progress.start();
		if (paramInt == 1) {
			this.ImageCreateTime = this.progress.getImageCreateTime();
		}

		i = JOptionPane.showOptionDialog(null, localJPanel, str2, 0, -1, null, arrayOfObject, arrayOfObject[0]);

		if (this.progress != null)
			this.progress.stopProgress();
		if (!this.progress.progress_complete) {
			if (this.progress.progress_result == -1) {
				setErrorMessage(LocaleStrings.getString("6_40_IUSBREDIR") + ", got null folder path");
			} else if (this.progress.progress_result == -2) {
				setErrorMessage(LocaleStrings.getString("6_40_IUSBREDIR")
						+ "\nRood Dir entry can not save too much file with FAT16\nPlease using FAT32. EX: Image Size is bigger than 2");
			} else
				setErrorMessage(LocaleStrings.getString("6_40_IUSBREDIR"));
			this.progress = null;
			System.gc();
			return false;
		}

		this.progress = null;
		System.gc();
		return true;
	}

	public boolean startRedirection(String paramString1, String paramString2, int paramInt1, String paramString3,
			int paramInt2, boolean paramBoolean) throws RedirectionException {
		if (this.running)
			return true;
		Object localObject2;
		if (!isPhysicalDevice()) {
			if (paramString2.indexOf(" : ") != -1) {
				this.ImageCreateTime = 0L;
				String[] localObject1 = paramString2.split(" : ");

				if (localObject1[2].endsWith(File.separator)) {
					localObject1[2] = (localObject1[2] + "AMI_" + paramInt1 + ".img");
				} else {
					localObject1[2] = (localObject1[2] + File.separator + "AMI_" + paramInt1 + ".img");
				}
				this.imageSize = Integer.parseInt(localObject1[0]);
				this.folderPath = localObject1[1];
				this.imagePath = localObject1[2];

				if (createImageProgressBar(1)) {
					paramString2 = this.imagePath;
					this.mount_folder_flag = true;
				} else {
					localObject2 = new File(this.imagePath);
					if (((File) localObject2).exists()) {
						((File) localObject2).delete();
					}

					return false;
				}
			}
		}

		this.HDDevice_no = paramInt1;
		Object localObject1 = null;
		localObject1 = VMApp.getInstance().getIUSBRedirSession();
		try {
			if (harddiskConnect(paramString1, paramInt2, paramBoolean) < 0)
				return false;
			SendAuth_SessionToken(paramString3);

			localObject2 = recvRequest();
			this.hdInstanceNum = ((IUSBSCSI) localObject2).instanceNum;

			if (((IUSBSCSI) localObject2).opcode == 241) {
				if (((IUSBSCSI) localObject2).connectionStatus == 5) {
					harddiskDisconnect();
					setErrorMessage(LocaleStrings.getString("4_17_CDROMREDIR"));
					return false;
				}
				if (((IUSBSCSI) localObject2).connectionStatus == 8) {
					harddiskDisconnect();
					setErrorMessage(LocaleStrings.getString("AB_8_HDREDIR") + ((IUSBSCSI) localObject2).m_otherIP);
					return false;
				}
				if (((IUSBSCSI) localObject2).connectionStatus == 13) {
					harddiskDisconnect();
					setErrorMessage(LocaleStrings.getString("F_136_JVM"));
					return false;
				}
				if (((IUSBSCSI) localObject2).connectionStatus != 1) {
					harddiskDisconnect();

					if (((IUSBSCSI) localObject2).m_otherIP != null) {
						if ((((IUSBSCSI) localObject2).m_otherIP.equalsIgnoreCase("127.0.0.1"))
								|| (((IUSBSCSI) localObject2).m_otherIP.equalsIgnoreCase("::1"))) {
							setErrorMessage(LocaleStrings.getString("4_19_CDROMREDIR"));
						} else
							setErrorMessage(
									LocaleStrings.getString("4_7_CDROMREDIR") + ((IUSBSCSI) localObject2).m_otherIP);
					}
					return false;
				}
			} else {
				harddiskDisconnect();
				throw new RedirectionException(
						LocaleStrings.getString("4_8_CDROMREDIR") + ((IUSBSCSI) localObject2).opcode);
			}
		} catch (IOException localIOException) {
			throw new RedirectionException(localIOException.getMessage());
		}

		if (this.nativeReaderPointer == -1L)
			newHarddiskReader(this.physicalDevice);
		Object localObject3;
		if (System.getProperty("os.name").startsWith("Windows")) {
			this.sourceHarddisk = (paramString2 = paramString2.trim());
			if (isPhysicalDevice()) {
				localObject3 = paramString2.substring(paramString2.indexOf('['));
				paramString2 = paramString2.substring(0, paramString2.indexOf('['));

				String str = "";
				int j = 0;
				char c;
				while (j < paramString2.lastIndexOf('-') + 1) {
					c = paramString2.charAt(j++);
					if (' ' != c) {
						str = str + c;
					}
				}
				paramString2 = str;

				paramString2 = paramString2.replaceFirst("-", "");
				j = 0;
				while (j < ((String) localObject3).indexOf(']')) {
					c = ((String) localObject3).charAt(j);
					if ((c != '[') && (c != ']') && (c != '-'))
						paramString2 = paramString2 + c;
					j++;
				}
				paramString2 = paramString2 + ((String) localObject3).substring(j + 1);
			}

		} else if (isPhysicalDevice()) {
			localObject3 = paramString2.split("-");
			this.sourceHarddisk = paramString2;
			paramString2 = ((String[]) localObject3)[0].trim();
		} else {
			this.sourceHarddisk = (paramString2 = paramString2.trim());
		}

		try {
			int i = openHarddisk(paramString2.getBytes("UTF-8"), isPhysicalDevice());
			if (i < 0) {
				if (i == -3) {
					if (((IUSBRedirSession) localObject1).isHarddiskPhysicalDrive(this.HDDevice_no)) {
						setErrorMessage(LocaleStrings.getString("6_37_IUSBREDIR"));
					} else {
						setErrorMessage(LocaleStrings.getString("5_8_FLOPPYREDIR"));
					}
				} else if ((i == -4) || (i == -1)) {
					setErrorMessage(LocaleStrings.getString("AB_11_HDREDIR"));
				}
				System.err.println(LocaleStrings.getString("AB_6_HDREDIR"));
				deleteHarddiskReader();
				harddiskDisconnect();
				return false;
			}
			if (i == 2) {
				if ((JViewer.getOEMFeatureStatus() & 0x2) == 2L) {
					if (((IUSBRedirSession) localObject1).isHarddiskPhysicalDrive(this.HDDevice_no)) {
						setErrorMessage(LocaleStrings.getString("6_37_IUSBREDIR"));
					} else {
						setErrorMessage(LocaleStrings.getString("5_11_FLOPPYREDIR"));
					}
					System.err.println(LocaleStrings.getString("AB_6_HDREDIR") + i);
					deleteHarddiskReader();
					harddiskDisconnect();
					return false;
				}

				if (!JViewer.isVMApp()) {
					if (!JViewerApp.getInstance().getRetryConnection()) {
						InfoDialog.showDialog(JViewerApp.getInstance().getVMDialog(),
								LocaleStrings.getString("4_13_CDROMREDIR"), LocaleStrings.getString("A_6_GLOBAL"), 1);
					}

				} else {
					InfoDialog.showDialog(VMApp.getVMFrame(), LocaleStrings.getString("4_13_CDROMREDIR"),
							LocaleStrings.getString("A_6_GLOBAL"), 1);
				}
			}
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
			System.out.println(LocaleStrings.getString("4_12_CDROMREDIR"));
		}
		if (!JViewer.isVMApp())
			JViewerApp.getInstance().getKVMClient().MediaRedirectionState((byte) 1);
		this.nBytesRedirected = 0;

		this.redirThread = new Thread(this);
		this.redirThread.start();
		if (((IUSBRedirSession) localObject1).isHarddiskPhysicalDrive(this.HDDevice_no)) {
			this.vMThread = new vHarddiskMonitorThread(this.HDDevice_no);
			this.vMThread.startharddiskMonitor();
		}
		this.running = true;
		this.harddiskRedirStatus = 1;
		return true;
	}

	public boolean stopRedirection() {
		if (this.running) {
			try {
				if (!this.hdServiceRestarted) {
					IUSBSCSI.sendCommandToMediaServer(this.packetMaster, this.packetWriteBuffer, null, 247);
				}
			} catch (Exception localException) {
				Debug.out
						.println("Sending MEDIA_SESSION_DISCONNECT command to media server failed : " + localException);
			}

			this.stopRunning = true;
			harddiskDisconnect();
			try {
				this.redirThread.join();
			} catch (InterruptedException localInterruptedException) {
				System.err.println(LocaleStrings.getString("AB_7_HDREDIR"));
			}
			if (!JViewer.isVMApp())
				JViewerApp.getInstance().getKVMClient().MediaRedirectionState((byte) 0);
			harddiskDisconnect();
			this.running = false;
			this.stopRunning = false;
			closeHarddisk();
			deleteHarddiskReader();

			if (this.mount_folder_flag) {
				createImageProgressBar(2);

				File localFile = new File(this.imagePath);
				if (localFile.exists()) {
					localFile.delete();
				}
				this.mount_folder_flag = false;
			}
		}
		this.nBytesRedirected = 0;
		this.harddiskRedirStatus = 0;
		return true;
	}

	private IUSBSCSI recvRequest() throws IOException, RedirectionException {
		return (IUSBSCSI) this.packetMaster.receivePacket();
	}

	public boolean isRedirActive() {
		return this.running;
	}

	public String getSourceDrive() {
		return this.sourceHarddisk;
	}

	public void stopRedirectionAbnormal() {
		if (this.running) {
			this.stopRunning = true;
			harddiskDisconnect();
			this.running = false;
			this.stopRunning = false;
			closeHarddisk();
			deleteHarddiskReader();
			JViewerApp.getInstance().reportHarddiskAbnormal(this.HDDevice_no);

			if (this.mount_folder_flag) {
				createImageProgressBar(2);

				File localFile = new File(this.imagePath);
				if (localFile.exists()) {
					localFile.delete();
				}
				this.mount_folder_flag = false;
			}
		}
		this.harddiskRedirStatus = 1;
	}

	public void run() {
		int i = 0;

		while (!this.stopRunning) {
			try {
				this.packetWriteBuffer.rewind();

				IUSBSCSI localIUSBSCSI1 = recvRequest();

				if (localIUSBSCSI1 != null) {

					int j = executeHarddiskSCSICmd(this.packetReadBuffer, this.packetWriteBuffer);
					this.packetWriteBuffer.limit(j);
					if (localIUSBSCSI1.opcode == 244) {
						this.packetWriteBuffer.position(11);
						this.packetWriteBuffer.putInt(31);
						j += 1;
						this.packetWriteBuffer.limit(j);
						this.packetWriteBuffer.position(this.packetWriteBuffer.limit() - 1);
						this.packetWriteBuffer.put((byte) getDrive_Type());
					}
					this.packetWriteBuffer.position(0);

					if (localIUSBSCSI1.opcode == 246) {

						this.hdRedirectionKilled = true;
						return;
					}
					if (localIUSBSCSI1.opcode == 247) {
						this.hdServiceRestarted = true;
						return;
					}

					IUSBSCSI localIUSBSCSI2 = new IUSBSCSI(this.packetWriteBuffer, true);

					this.packetMaster.sendPacket(localIUSBSCSI2);
					i += j;
					this.nBytesRedirected += i / 1024;
					i %= 1024;

					if ((localIUSBSCSI1.opcode == 27) && (localIUSBSCSI1.Lba == 2))
						this.hdImageEjected = true;
				}
			} catch (Exception localException) {
				Debug.out.println(localException);
				if (!this.stopRunning) {
					if ((JViewer.isKVMReconnectEnabled())
							&& (JViewerApp.getInstance().GetRedirectionState() != JViewerApp.REDIR_STOPPING)
							&& (JViewerApp.getInstance().GetRedirectionState() != JViewerApp.REDIR_STOPPED)) {

						if (JViewerApp.getInstance().getRetryConnection()) {

							Debug.out.println(
									"\n Harddisk redierction thread returned .because of KVM Reconnect in progress\n");
							return;
						}
					}

					synchronized (getSyncObj()) {
						try {
							if (!this.confModified) {
								getSyncObj().wait(10000L);
								if (this.hdStoppedByUser == true) {

									return;
								}
							}
						} catch (InterruptedException localInterruptedException) {
							localInterruptedException.printStackTrace();
						}
					}
					if (!this.confModified) {
						handleError(LocaleStrings.getString("AB_1_HDREDIR"));
					} else
						this.confModified = false;
					stopRedirectionAbnormal();
					return;
				}
			}
		}
	}

	public String[] getEntireHarddiskList() {
		if (!this.physicalDevice) {
			DisplayErrorMsg("Cannot get Hard disk drive list during Hard disk IMAGE redirection");
			return null;
		}

		if (this.nativeReaderPointer == -1L) {
			newHarddiskReader(true);
		}
		String[] arrayOfString1 = listHardDrives();
		String[] arrayOfString2 = listHardDrivesFixed();
		String[] arrayOfString3 = null;

		if ((arrayOfString1 != null) && (arrayOfString2 != null)) {
			arrayOfString3 = new String[arrayOfString1.length + arrayOfString2.length];
		} else if (arrayOfString2 == null) {
			arrayOfString3 = new String[arrayOfString1.length];
		} else if (arrayOfString1 == null) {
			arrayOfString3 = new String[arrayOfString2.length];
		}
		int i = 0;
		int j;
		if (arrayOfString1 != null) {
			for (j = 0; j < arrayOfString1.length; j++) {
				arrayOfString3[i] = arrayOfString1[j];
				i++;
			}
		}
		if (arrayOfString2 != null) {
			for (j = 0; j < arrayOfString2.length; j++) {
				arrayOfString3[i] = arrayOfString2[j];
				i++;
			}
		}
		return arrayOfString3;
	}

	public String[] getUSBHDDList() {
		if (!this.physicalDevice) {
			DisplayErrorMsg("Cannot get Hard Disk drive list during hard disk IMAGE redirection");
			return null;
		}

		if (this.nativeReaderPointer == -1L) {
			newHarddiskReader(true);
		}
		String[] arrayOfString = listHardDrives();

		if (arrayOfString == null)
			return arrayOfString;
		int i;
		String str1;
		String str2;
		if (System.getProperty("os.name").startsWith("Win")) {
			for (i = 0; i < arrayOfString.length; i++) {
				str1 = arrayOfString[i];
				str2 = str1.substring(0, 1);
				String str3 = str1.substring(2);
				String str4 = "[";

				int j = 0;
				while (j < str3.length() - 1) {
					str4 = str4 + str3.charAt(j) + "-";
					j++;
				}
				String str5;
				if (str3.charAt(j) == 'l') {
					str4 = str4.substring(0, str4.length() - 1);
					str4 = str4 + "]";
					str5 = LOGICAL_DRIVE;
				} else {
					str4 = str4 + str3.charAt(j) + "]";
					str5 = PHYSICAL_DRIVE;
				}
				String str6 = str5.concat("-").concat(str2).concat("-").concat(str4);
				arrayOfString[i] = str6;
			}

		} else if (System.getProperty("os.name").equals("Linux")) {
			for (i = 0; i < arrayOfString.length; i++) {
				str1 = arrayOfString[i];
				str2 = str1.substring(0, str1.length() - 1);
				arrayOfString[i] = str2;
			}
		}

		return arrayOfString;
	}

	public String[] getHarddiskFixedList() {
		if (!this.physicalDevice) {
			DisplayErrorMsg("Cannot get hard disk drive list during Hard disk IMAGE redirection");
			return null;
		}

		if (this.nativeReaderPointer == -1L) {
			newHarddiskReader(true);
		}
		String[] arrayOfString = listHardDrivesFixed();

		if (arrayOfString == null)
			return null;
		int i;
		String str1;
		String str2;
		if (System.getProperty("os.name").startsWith("Win")) {
			for (i = 0; i < arrayOfString.length; i++) {
				str1 = arrayOfString[i];
				str2 = str1.substring(0, 1);
				String str3 = str1.substring(2);
				String str4 = "[";

				int j = 0;
				while (j < str3.length() - 1) {
					str4 = str4 + str3.charAt(j) + "-";
					j++;
				}
				String str5;
				if (str3.charAt(j) == 'l') {
					str4 = str4.substring(0, str4.length() - 1);
					str4 = str4 + "]";
					str5 = LOGICAL_DRIVE;
				} else {
					str4 = str4 + str3.charAt(j) + "]";
					str5 = PHYSICAL_DRIVE;
				}
				String str6 = str5.concat("-").concat(str2).concat("-").concat(str4);
				arrayOfString[i] = str6;
			}

		} else if (System.getProperty("os.name").equals("Linux")) {
			for (i = 0; i < arrayOfString.length; i++) {
				str1 = arrayOfString[i];
				str2 = str1.substring(0, str1.length() - 1);
				arrayOfString[i] = str2;
			}
		}
		return arrayOfString;
	}

	public String getLIBHARDDISKVersion() {
		String str;

		if (this.nativeReaderPointer == -1L) {
			newHarddiskReader(false);
			str = getVersion();
			deleteHarddiskReader();
		} else {
			str = getVersion();
		}
		return str;
	}

	public void DisplayErrorMsg(String paramString) {
		if (JViewer.isVMApp()) {
			VMApp.getInstance().generalErrorMessage(LocaleStrings.getString("4_16_CDROMREDIR"), paramString);
		} else {
			JViewerApp.getInstance().getMainWindow().generalErrorMessage(LocaleStrings.getString("4_16_CDROMREDIR"),
					paramString);
		}
	}

	public void handleError(String paramString) {
		DisplayErrorMsg(paramString);
	}

	public boolean isPhysicalDevice() {
		return this.physicalDevice;
	}

	public void SendAuth_SessionToken(String paramString) throws RedirectionException, IOException {
		int i = 0;
		int j = JViewerApp.getInstance().getSessionTokenType();

		if (j == 0) {
			i = 128;
			this.packetWriteBuffer.clear();
			this.packetWriteBuffer.limit(160);
		} else if (j == 1) {
			i = 240;
			this.packetWriteBuffer.clear();
			this.packetWriteBuffer.limit(272);
		}

		IUSBHeader localIUSBHeader = new IUSBHeader(i);
		localIUSBHeader.write(this.packetWriteBuffer);
		this.packetWriteBuffer.position(41);
		this.packetWriteBuffer.put((byte) -14);
		this.packetWriteBuffer.position(62);
		this.packetWriteBuffer.put((byte) 0);
		this.packetWriteBuffer.put(paramString.getBytes());
		this.packetWriteBuffer.position(22);
		this.packetWriteBuffer.put((byte) getDrive_Type());
		this.packetWriteBuffer.put((byte) this.HDDevice_no);
		this.packetWriteBuffer.position(0);
		IUSBSCSI localIUSBSCSI = new IUSBSCSI(this.packetWriteBuffer, true);
		this.packetMaster.sendPacket(localIUSBSCSI);
	}

	public int getBytesRedirected() {
		return this.nBytesRedirected;
	}

	public int getDrive_Type() {
		Debug.out.println("GEt DRIVE TYPE:" + this.Drive_Type);
		return this.Drive_Type;
	}

	public static Object getSyncObj() {
		return syncObj;
	}

	public boolean isConfModified() {
		return this.confModified;
	}

	public void setConfModified(boolean paramBoolean) {
		this.confModified = paramBoolean;
	}

	public void setDrive_Type(int paramInt) {
		this.Drive_Type = paramInt;
	}

	public int getHdInstanceNum() {
		return this.hdInstanceNum;
	}

	public boolean isHdImageRedirected() {
		return this.hdImageRedirected;
	}

	public void setHdImageRedirected(boolean paramBoolean) {
		this.hdImageRedirected = paramBoolean;
	}

	public boolean isHdImageEjected() {
		return this.hdImageEjected;
	}

	public boolean isHdRedirectionKilled() {
		return this.hdRedirectionKilled;
	}

	public void setHdReconnect(boolean paramBoolean) {
		this.hdReconnect = paramBoolean;
	}

	public boolean getHdReconnect() {
		return this.hdReconnect;
	}

	public boolean isHdStoppedByUser() {
		return this.hdStoppedByUser;
	}

	public void setHdStoppedByUser(boolean paramBoolean) {
		this.hdStoppedByUser = paramBoolean;
	}

	public String getErrorMessage() {
		return this.errorMessage;
	}

	public void setErrorMessage(String paramString) {
		this.errorMessage = paramString;
	}

	public void setHdRedirectionKilled(boolean paramBoolean) {
		this.hdRedirectionKilled = paramBoolean;
	}

	public int getHarddiskRedirStatus() {
		return this.harddiskRedirStatus;
	}

	public void setHarddiskRedirStatus(int paramInt) {
		this.harddiskRedirStatus = paramInt;
	}

	public int getHardDiskDeviceStatus() {
		return this.hardDiskDeviceStatus;
	}

	public void setHardDiskDeviceStatus(int paramInt) {
		this.hardDiskDeviceStatus = paramInt;
	}

	public int getHdStopMode() {
		return this.hdStopMode;
	}

	public void setHdStopMode(int paramInt) {
		this.hdStopMode = paramInt;
	}

	public boolean isHdServiceRestarted() {
		return this.hdServiceRestarted;
	}

	private static void showLibraryLoadError() {
		if (JViewer.isVMApp()) {
			JOptionPane.showMessageDialog(VMApp.getVMFrame(), LocaleStrings.getString("5_13_FLOPPYREDIR"),
					LocaleStrings.getString("1_3_JVIEWER"), 0);

			VMApp.exit(0);
		}
	}

	private native String[] listHardDrivesFixed();

	private native String[] listHardDrives();

	private native void newHarddiskReader(boolean paramBoolean);

	private native void deleteHarddiskReader();

	private native int openHarddisk(byte[] paramArrayOfByte, boolean paramBoolean);

	private native void closeHarddisk();

	private native int executeHarddiskSCSICmd(ByteBuffer paramByteBuffer1, ByteBuffer paramByteBuffer2);

	private native String getVersion();
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\iusb\HarddiskRedir.class Java
 * compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */